package com.woniu.utils;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jiang
 */
@Data
@NoArgsConstructor
public class PageInfo<T> {
    private int total;
    private int pages;
    private int pageNum;
    private int pageSize;
    private List<T> list;

    public PageInfo(Page<?> page, Class<T> clazz) {
        this.total = ((int) page.getTotal());
        this.pages = ((int) page.getPages());
        this.pageNum = ((int) page.getCurrent());
        this.pageSize = ((int) page.getSize());
        List<?> records = page.getRecords();
        List<T> list = new ArrayList<T>();
        for (Object record : records) {
            T bean = BeanUtil.toBean(record, clazz);
            list.add(bean);
        }
        this.setList(list);
    }
}
